using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Library.Components.Lang;
using Framework.Module.Version;
using Framework.Library.Components.Commands;



namespace Portal.Administrator.UserControls
{
    public partial class NavPanel : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblCurrentUser.Text = CurrentUser.DisplayName;                
                DataTable dtLang = MainLanguagesController.GetAll();
                if (dtLang != null && dtLang.Rows.Count > 0 && AppEnv.AllowedLanguage() != "None")
                {
                    rptLanguage.DataSource = dtLang;
                    rptLanguage.ItemDataBound += new RepeaterItemEventHandler(rptLanguage_ItemDataBound);
                    rptLanguage.DataBind();                    
                }
                else
                {
                    rptLanguage.Visible = false;
                }                
                PortalController.LoadPortalsForUserID(dropPortal.Items, CurrentUser.UserID);
                if (ConvertUtility.ToInt32(AppEnv.PortalId()) != 0)
                    dropPortal.SelectedValue = AppEnv.PortalId();
                hplChangePass.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.UserProfile)) + "&uid=" + CurrentUser.UserID;
                lblEDITPORTAL.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.PortalManage)) + "&edit=True";
                lblNEWPORTAL.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.PortalManage));
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            AuthenticateUtility.LogoutUser();
            Response.Redirect(AppEnv.ADMIN_CMD);
        }      

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "portalid", dropPortal.SelectedValue));
        }

        protected void rptLanguage_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                LinkButton lbalias = (LinkButton)e.Item.FindControl("hplLanguage");                
                lbalias.Text = (ConvertUtility.ToString(currow["Language_Name"]));
                lbalias.CommandName = "ChangeLanguage";                
                if (currow["Language_Culture"].ToString() == AppEnv.GetLanguage())
                {
                    lbalias.CssClass = "LinkActive";
                }
                lbalias.CommandArgument = (ConvertUtility.ToString(currow["Language_Culture"]));
            }
        }

        protected void rptLanguage_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ChangeLanguage")
            {
                AppEnv.SetLanguage(e.CommandArgument.ToString());
                string curUrl = Request.RawUrl;
                string newUrl = string.Empty;
                if (curUrl.IndexOf("&lang") > -1)
                    newUrl = curUrl.Substring(0, curUrl.IndexOf("&lang"));
                else if (curUrl.IndexOf("?lang") > -1)
                    newUrl = curUrl.Substring(0, curUrl.IndexOf("?lang"));
                else
                    newUrl = curUrl;                
                PortalController.LoadPortals(dropPortal.Items);
                Framework.Library.Components.DataCaching data = new Framework.Library.Components.DataCaching();
                data.RemoveCache("Framework.Portal.Command");
                Response.Redirect(newUrl);                
            }            
        }
    }
}